home *** CD-ROM | disk | FTP | other *** search
- /*
-
- CTCP.c
- Superclass: CMacTCPDriver
-
- The TCP implementation (Chapter 4, MacTCP Programmer's Guide).
-
- Copyright © NCSA, University of Illinois; June 2, 1992
- Eric Johnson, John Newlin and Igor Livshits
-
- This code may be used, modified, and distributed free of charge and obligation.
-
- */
-
-
- #include "CTCP.h"
-
- /*=====================*/
- /*===---------------===*/
-
- OSErr CTCP::ITCP(void)
- Begin
- CMacTCPDriver::IMacTCPDriver(); // Initialize the driver
- // Initialize all instance variables
- remoteAddress= Null;
- localAddress= Null;
- destinationPort= Null;
- sourcePort= Null;
- *remoteHostName= kEOL;
- streamID= Null;
- streamLength= Null;
- streamBuffer= Null;
- inBuffer= Null;
- inDataLength= Null;
- outBuffer= Null;
- outDataLength= Null;
- timeOutDelay= kTimeOut;
- fMark= False;
- fUrgent= False;
- itsLastError= noErr;
- End
-
- /*===---------------===*/
-
- void CTCP::Dispose(void)
- Begin
- Release();
-
- inherited::Dispose();
- End
-
- /*===---------------===*/
-
- void CTCP::SetTimeOut(byte timeOut)
- Begin
- timeOutDelay= timeOut; // Time out delay
- End
-
- /*===---------------===*/
-
- byte CTCP::GetTimeOut(void)
- Begin
- return timeOutDelay; // Current time out delay
- End
-
- /*===---------------===*/
-
- void CTCP::SetDestination(char* name, ip_addr address, ip_port port)
- Begin
- MoveHHi((Handle)this);
- HLock((Handle)this); // Lock our object
-
- strncpy(remoteHostName, name, kNameLength);
- remoteAddress= address;
- destinationPort= port;
-
- HUnlock((Handle)this);
- End
-
- /*===---------------===*/
-
- void CTCP::GetDestination(char* name, ip_addr* address, ip_port* port)
- Begin
- MoveHHi((Handle)this);
- HLock((Handle)this); // Lock our object
-
- strncpy(name, remoteHostName, kNameLength);
- *address= remoteAddress;
- *port= destinationPort;
-
- HUnlock((Handle)this);
- End
-
- /*===---------------===*/
-
- void CTCP::Wait(void)
- Begin
- return;
- End
-
- /*===---------------===*/
-
- void CTCP::MakeStreamBuffer(Size length)
- Begin
- Ptr buffer;
-
- streamLength= length;
-
- ForgetPtr(streamBuffer);
- buffer= NewPtrClear(streamLength);
- streamBuffer= buffer;
- End
-
- /*===---------------===*/
-
- void CTCP::PlaceData(Ptr data, Size length)
- Begin
- Ptr theData;
-
- ForgetPtr(outBuffer); // Release previous buffer
-
- TRY
- {
- theData= NewPtrClear(length);
- outBuffer= theData;
-
- BlockMove(data, outBuffer, length);
- outDataLength= length;
- }
- CATCH
- {
- ForgetPtr(outBuffer);
- outDataLength= Null;
- }
- ENDTRY
- End
-
- /*===---------------===*/
-
- void CTCP::CreateInputBuffer(Ptr* data, Size length)
- Begin
- Ptr theData;
-
- TRY
- {
- theData= NewPtrClear(length); // We need to allocate a new one
- *data= inBuffer= theData;; // Make sure the rest of the program can see it
- inDataLength= length;
- }
- CATCH
- {
- ForgetPtr(inBuffer);
- inDataLength= Null;
- }
- ENDTRY
- End
-
- /*===---------------===*/
-
- OSErr CTCP::SetInputBuffer(Ptr data, Size length)
- Begin
- if (data)
- { // Set to passed buffer
- inBuffer= data;
- inDataLength= length;
- return noErr;
- }
- else
- return kInvalidBufPtr; // Null pointer
- End
-
- /*===---------------===*/
-
- Size CTCP::GetInputLength(void)
- Begin
- return inDataLength;
- End
-
- /*===--------------===*/
-
- Ptr CTCP::GetInputBuffer(void)
- Begin
- return inBuffer; // Assume dynamic allocation
- End
-
- /*===--------------===*/
-
- void CTCP::ReleaseStreamBuffer(void)
- Begin
- ForgetPtr(streamBuffer); // Release the buffer
- streamLength= Null; // We have nothing!
- End
-
- /*===--------------===*/
-
- void CTCP::ReleaseInputBuffer(void)
- Begin
- ForgetPtr(inBuffer); // Release the buffer
- inDataLength= Null; // We have nothing!
- End
-
- /*===--------------===*/
-
- void CTCP::ReleaseOutputBuffer(void)
- Begin
- ForgetPtr(outBuffer); // Release the buffer
- outDataLength= Null; // We have nothing!
- End
-
- /*===--------------===*/
-
- void CTCP::ReleaseBuffers(void)
- Begin
- ReleaseOutputBuffer();
- ReleaseInputBuffer();
- ReleaseStreamBuffer();
- End
-
- /*===--------------===*/
-
- OSErr CTCP::GetLastError(void)
- Begin
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::Create(void)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPCreate;
- parameters->ioResult= kInProgress;
-
- parameters->csParam.create.rcvBuff= streamBuffer;
- parameters->csParam.create.rcvBuffLen= streamLength;
- parameters->csParam.create.notifyProc= Null;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- Process(); // Let it run
-
- GetParameters((ParmBlkPtr*)¶meters);
- streamID= parameters->tcpStream;
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::PassiveOpen(void)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPPassiveOpen;
- parameters->ioResult= kInProgress;
-
- parameters->csParam.open.ulpTimeoutValue= timeOutDelay;
- parameters->csParam.open.ulpTimeoutAction= kDefaultAction;
- parameters->csParam.open.validityFlags= kDefaultFlags;
- parameters->csParam.open.commandTimeoutValue= timeOutDelay;
- parameters->csParam.open.remoteHost= Null;
- parameters->csParam.open.remotePort= destinationPort;
- parameters->csParam.open.localPort= sourcePort;
- parameters->csParam.open.tosFlags= Null;
- parameters->csParam.open.precedence= Null;
- parameters->csParam.open.dontFrag= False;
- parameters->csParam.open.timeToLive= Null;
- parameters->csParam.open.security= Null;
- parameters->csParam.open.optionCnt= Null;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- if (!fAsynchronous)
- Process(); // Let it run
-
- GetParameters((ParmBlkPtr*)¶meters);
- remoteAddress= parameters->csParam.open.remoteHost;
- destinationPort= parameters->csParam.open.remotePort;
- localAddress= parameters->csParam.open.localHost;
- sourcePort= parameters->csParam.open.localPort;
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::ActiveOpen(void)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPActiveOpen;
- parameters->ioResult= kInProgress;
-
- parameters->csParam.open.ulpTimeoutValue= timeOutDelay;
- parameters->csParam.open.ulpTimeoutAction= kDefaultAction;
- parameters->csParam.open.validityFlags= kDefaultFlags;
- parameters->csParam.open.commandTimeoutValue= timeOutDelay;
- parameters->csParam.open.remoteHost= remoteAddress;
- parameters->csParam.open.remotePort= destinationPort;
- parameters->csParam.open.localPort= sourcePort;
- parameters->csParam.open.tosFlags= Null;
- parameters->csParam.open.precedence= Null;
- parameters->csParam.open.dontFrag= False;
- parameters->csParam.open.timeToLive= Null;
- parameters->csParam.open.security= Null;
- parameters->csParam.open.optionCnt= Null;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- if (!fAsynchronous)
- Process(); // Let it run
-
- GetParameters((ParmBlkPtr*)¶meters);
- localAddress= parameters->csParam.open.localHost;
- sourcePort= parameters->csParam.open.localPort;
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::Send(void)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
- struct wdsEntry myWDS[2]; // Global write data structure
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- myWDS[0].length= outDataLength; // Data to send over the link
- myWDS[0].ptr= outBuffer;
- myWDS[1].length= Null;
- myWDS[1].ptr= Null;
-
- parameters->csCode= TCPSend;
- parameters->ioResult= kInProgress;
-
- parameters->csParam.send.ulpTimeoutValue= timeOutDelay;
- parameters->csParam.send.ulpTimeoutAction= kDefaultAction;
- parameters->csParam.send.validityFlags= kDefaultFlags;
- parameters->csParam.send.pushFlag= False;
- parameters->csParam.send.urgentFlag= False;
- parameters->csParam.send.wdsPtr= (Ptr)myWDS;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- if (!fAsynchronous)
- Process(); // Let it run
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::NoCopyRcv(Ptr rdsPtr, short rdsLength)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPNoCopyRcv;
- parameters->ioResult= kInProgress;
-
- parameters->csParam.receive.commandTimeoutValue= timeOutDelay;
- parameters->csParam.receive.rdsPtr= rdsPtr;
- parameters->csParam.receive.rdsLength= rdsLength;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- if (!fAsynchronous)
- Process(); // Let it run
-
- GetParameters((ParmBlkPtr*)¶meters);
- fUrgent= parameters->csParam.receive.urgentFlag;
- fMark= parameters->csParam.receive.markFlag;
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::BfrReturn(Ptr rdsPtr)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPRcvBfrReturn;
- parameters->ioResult= kInProgress;
-
- parameters->csParam.receive.rdsPtr= rdsPtr;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- Process(); // Let it run
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::Rcv(void)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPRcv;
- parameters->ioResult= kInProgress;
-
- parameters->csParam.receive.commandTimeoutValue= timeOutDelay;
- parameters->csParam.receive.rcvBuff= inBuffer;
- parameters->csParam.receive.rcvBuffLen= inDataLength;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- if (!fAsynchronous)
- Process(); // Let it run
-
- GetParameters((ParmBlkPtr*)¶meters);
- inDataLength= parameters->csParam.receive.rcvBuffLen;
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::Close(void)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPClose;
- parameters->ioResult= kInProgress;
-
- parameters->csParam.close.ulpTimeoutValue= timeOutDelay;
- parameters->csParam.close.validityFlags= kDefaultFlags;
- parameters->csParam.close.ulpTimeoutAction= kDefaultAction;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- Process(); // Let it run
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::Abort(void)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPAbort;
- parameters->ioResult= kInProgress;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- Process(); // Let it run
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::Release(void)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPRelease;
- parameters->ioResult= kInProgress;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- Process(); // Let it run
-
- GetParameters((ParmBlkPtr*)¶meters);
- streamBuffer= parameters->csParam.create.rcvBuff;
- streamLength= parameters->csParam.create.rcvBuffLen;
-
- if (itsLastError == noErr)
- ReleaseBuffers();
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::Status(TCPStatusPB* theStatus)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPStatus;
- parameters->ioResult= kInProgress;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- Process(); // Let it run
-
- GetParameters((ParmBlkPtr*)¶meters);
- BlockMove((Ptr)&(parameters->csParam.status), (Ptr)theStatus, sizeof(TCPStatusPB));
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- OSErr CTCP::GlobalInfo(TCPParam* tcpParam, TCPStats* tcpStat)
- Begin
- TCPiopb pb; // Our parameters
- TCPiopbPtr parameters= &pb;
-
- GetParameters((ParmBlkPtr*)¶meters);
-
- parameters->csCode= TCPGlobalInfo;
- parameters->ioResult= kInProgress;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- Process(); // Let it run
-
- GetParameters((ParmBlkPtr*)¶meters);
- BlockMove(parameters->csParam.globalInfo.tcpStatsPtr, tcpStat, sizeof(TCPStats));
- BlockMove(parameters->csParam.globalInfo.tcpParamPtr, tcpParam, sizeof(TCPParam));
-
- return itsLastError;
- End
-
- /*===---------------===*/
-
- ip_addr CTCP::GetMyIPAddress(void)
- Begin
- if (!localAddress)
- { // Fetch our address
- TCPiopb pb, pbStorage; // Our MacTCP parameters
- TCPiopbPtr parameters= &pbStorage;
-
-
- GetParameters((ParmBlkPtr*)¶meters);// For storage
-
- parameters= &pb; // Working parameters
- GetParameters((ParmBlkPtr*)¶meters);// Get another copy for actual use
- parameters->csCode= ipctlGetAddr;
- parameters->ioResult= kInProgress;
-
- SetParameters((ParmBlkPtr)parameters);
- SendControlInfoToDriver(); // Set our action...
- Process(); // Let it run
-
- GetParameters((ParmBlkPtr*)¶meters);
- localAddress= ((IPParamBlockPtr)parameters)->ourAddress;
-
- SetParameters((ParmBlkPtr)&pbStorage); // Blast the original block back to resotore the streamPtr
- }
-
- return localAddress;
- End
-
- /*===---------------===*/
-
- void CTCP::SetSizeOfParameters(void)
- Begin
- this->sizeOfParameters= sizeof(TCPiopb);
- End
-
- /*===---------------===*/
- /*=====================*/